package com.tuanzi.loan.business.service.family.impl;

import com.tuanzi.loan.business.repository.family.UpdateVersionInfoRepository;
import com.tuanzi.loan.business.repository.family.UpdateVersionRepository;
import com.tuanzi.loan.business.service.family.UpdateVersionService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.UpdateVersionSearchRequest;
import com.tuanzi.loan.vo.response.family.UpdateVersionSearchResponse;
import com.tuanzi.loan.vo.view.family.UpdateVersionView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class UpdateVersionServiceImpl implements UpdateVersionService {

	@Autowired
	private UpdateVersionRepository updateVersionRepository;

	@Autowired
	private UpdateVersionInfoRepository updateVersionInfoRepository;

	@Override
	public Page<UpdateVersionSearchResponse> list(PageSearcher<UpdateVersionSearchRequest> pageSearcher) {
		return updateVersionRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	@Override
	public void online(Integer id) {
		updateVersionRepository.updateStatus(id, 1);
	}

	@Override
	public UpdateVersionView findById(Integer id) {
		return updateVersionRepository.findById(id);
	}

	@Override
	public UpdateVersionView save(UpdateVersionView view) {
		List<String> prdIds = view.getPrdIds();
		if (view.getStatus() == null) {
			view.setStatus((byte) 0);
		}
		UpdateVersionView saved = updateVersionInfoRepository.save(view);
		saved.setPrdIds(prdIds);
		return updateVersionRepository.save(view);
	}
}
